﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

public partial class Admin_Category_CategoryAdd : BasePage
{
    protected int labelId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!int.TryParse(Request.QueryString["labelid"], out labelId))
            return;
        PageInfo.Where = " where labelid="+labelId.ToString();
        PageInfo.OrderBy = " order by sort desc";
        if (!IsPostBack)
            BindData();
    }
    protected void BindData()
    {
        sort.Text = "0";

        MatchIns.BLL.TCategory bll = new MatchIns.BLL.TCategory();
        List<MatchIns.Model.TCategory> ds = new List<MatchIns.Model.TCategory>();
        bll.GetList(0, bll.GetPageList(PageInfo).List, ds, "");

        parentId.Items.Clear();
        parentId.DataSource = ds;
        parentId.DataTextField = "CategoryName";
        parentId.DataValueField = "CategoryID";
        parentId.DataBind();
        parentId.Items.Insert(0, new ListItem("请选择..."));


    }

    protected void save_Click(object sender, EventArgs e)
    {
        #region 赋值
        MatchIns.Model.TCategory model = new MatchIns.Model.TCategory();
        model.CategoryName = categoryName.Text.Trim();
        int _parentid;
        int _sort;
        int.TryParse(parentId.SelectedValue, out _parentid);
        model.ParentID = _parentid;
        int.TryParse(sort.Text, out _sort);
        model.Sort = _sort;
        model.LabelID = labelId;
        model.Description = intro.Value;

        if (photo.HasFile)
        {
            try
            {
                string fileName = MatchIns.Common.Upload.UploadImage(photo, "~/UploadFile/Category/", 100);
                model.Photo = fileName;
            }
            catch (Exception ex)
            {
                errors.Text = ex.Message;
            }

        }
        #endregion

        #region 验证
        errors.Items.Clear();

        if (model.CategoryName.Length == 0)
            errors.Items.Add("填写分类名称");

        if (errors.Items.Count > 0)
            return;

        #endregion


        #region 添加至数据库
        new MatchIns.BLL.TCategory().Add(model);
        errors.Items.Add("分类:"+model.CategoryName+",添加成功");
        #endregion

        #region 初始化控件
        categoryName.Text = "";
        BindData();
        #endregion

    }
    protected void cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("CategoryList.aspx?labelid="+labelId.ToString());
    }
}
